package com.ruoyi.web.listener;

import com.bye.xiaomi_mall.domain.TdOrder;
import com.bye.xiaomi_mall.domain.TdOrderDetail;
import com.bye.xiaomi_mall.mapper.TdGoodsMapper;
import com.bye.xiaomi_mall.mapper.TdOrderDetailMapper;
import com.bye.xiaomi_mall.mapper.TdOrderMapper;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
public class OrderCancelRabbitListener {
    @Autowired
    private TdOrderMapper tdOrderMapper;
    @Autowired
    private TdOrderDetailMapper tdOrderDetailMapper;
    @Autowired
    private TdGoodsMapper tdGoodsMapper;


//    配置延迟交换机
    @RabbitListener(
            bindings = @QueueBinding(
            exchange = @Exchange(name = "payback.order_delay_exchange", type = ExchangeTypes.FANOUT, delayed = "true"),
            value = @Queue(name = "order_delay_exchange", durable = "true"),
            key = "delay"
    ))
    public void orderCancelListen(String orderCode) {
          TdOrder order= tdOrderMapper.selectTdOrderByOrderCode(orderCode);
          if (null!=order&& Objects.equals(order.getOrderStatus(), "0")){
              //        更改订单状态为取消
              tdOrderMapper.updateTdOrderStatus(orderCode);
              //        返回商品的库存
              List<TdOrderDetail> list = tdOrderDetailMapper.selectOrderDetail(orderCode);
              for (TdOrderDetail orderDetail:list){
                  tdGoodsMapper.updateGoodsInventory2(orderDetail);
              }
              System.out.println("用户未付款 订单已取消");
              System.out.println("用户未付款 订单已取消");
              System.out.println("用户未付款 订单已取消");
              System.out.println("用户未付款 订单已取消");
              System.out.println("用户未付款 订单已取消");
              System.out.println("用户未付款 订单已取消");
          }
          else System.out.println("用户已付款");
    }
}



